﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Framework.Platform.Components.Exceptions;
using Framework.Platform.Components.UI.Field;

namespace Framework.Platform.Components.UI.View
{
    /// <summary>
    /// 视图基类，出于开发原因，没有把它设置为abstract。
    /// </summary>
    public partial class BaseView : UserControl, IView
    {
        private IDictionary<string, IField> _FieldsMap = new Dictionary<string, IField>();

        private IList<IField> _Fields;

        public IList<IField> Fields
        {
            get {
                if (_Fields == null)
                {
                    _Fields = new List<IField>();
                }
                return _Fields; }
            set { _Fields = value; }
        }

        private IDictionary<string, ViewCommander> _CommandersMap = new Dictionary<string, ViewCommander>();

        private IList<ViewCommander> _Commanders;

        public IList<ViewCommander> Commanders
        {
            get
            {
                if (_Commanders == null)
                {
                    _Commanders = new List<ViewCommander>();
                }
                return _Commanders;
            }
            set { _Commanders = value; }
        }
        
        #region 构造函数
        public BaseView()
        {
            this._ViewKey = this.GetType().FullName;
            this.InitializeComponent();
            this.Load += new EventHandler(BaseView_Load);
        }
        #endregion
        
        private void InitializeViewUI()
        {
            foreach (ViewCommander viewCommander in Commanders)
            {
                if (!this._CommandersMap.ContainsKey(viewCommander.CommanderKey))
                {
                    this._CommandersMap.Add(viewCommander.CommanderKey, viewCommander);
                }
            }

            foreach (IField field in Fields)
            {
                if (!this._FieldsMap.ContainsKey(field.FieldKey))
                {
                    this._FieldsMap[field.FieldKey] = field;
                }
            }
        }

        #region IView Members
        private string _ViewKey;
        public string ViewKey
        {
            get
            {
                return _ViewKey;
            }
            set
            {
                _ViewKey = value;
            }
        }

        private string _ViewName;
        public string ViewName
        {
            get { return _ViewName; }
            set { _ViewName = value; }
        }

        private ViewPosition _Position = ViewPosition.Middle;
        public ViewPosition Position
        {
            get
            {
                return _Position;
            }
            set
            {
                _Position = value;
            }
        }

        private bool _Closeable = true;
        public bool Closeable
        {
            get
            {
                return _Closeable;
            }
            set
            {
                _Closeable = value;
            }
        }

        private object _ViewDataSource;
        public object ViewDataSource
        {
            get { return _ViewDataSource; }
            set { _ViewDataSource = value; }
        }

        private object _InitViewData;
        public object InitViewData
        {
            get { return _InitViewData; }
            set { _InitViewData = value; }
        }

        private ViewStatus _Status;
        public ViewStatus Status
        {
            get { return _Status; }
            set { _Status = value; }
        }

        private bool _ReadOnly;
        public bool ReadOnly
        {
            get { return _ReadOnly; }
            set
            {
                if (_ReadOnly != value)
                {
                    foreach (IField field in this._FieldsMap.Values)
                    {
                        field.ReadOnly = value;
                    }
                    _ReadOnly = value;
                }
            }
        }

        public ICollection<IField> GetFields()
        {
            return this._FieldsMap.Values;
        }

        public IField GetFieldByKey(string fieldKey)
        {
            if (this._FieldsMap.ContainsKey(fieldKey))
            {
                return this._FieldsMap[fieldKey];
            }
            else
            {
                return null;
            }
        }

        public void AddField(IField field)
        {
            if (!this._FieldsMap.ContainsKey(field.FieldKey))
            {
                this.Fields.Add(field);
                this._FieldsMap[field.FieldKey] = field;
            }
        }

        public void RemoveFieldByKey(string fieldKey)
        {
            IField field = this._FieldsMap[fieldKey];
            if (field != null)
            {
                this.Fields.Remove(field);
                this._FieldsMap.Remove(fieldKey);
            }
        }

        public virtual void ViewDataBind()
        {
            foreach (IField field in this._FieldsMap.Values)
            {
                field.FieldDataBind();
            }
        }

        public virtual void ViewValidate()
        {
            ViewValidateException viewException = null;
            foreach (IField field in _FieldsMap.Values)
            {
                try
                {
                    field.FieldValidate();
                }
                catch (FieldValidateException ex)
                {
                    if (viewException == null)
                    {
                        viewException = new ViewValidateException();
                        viewException.View = this;
                    }
                    viewException.FieldExceptions.Add(ex);
                }
                catch
                {
                    throw;
                }
            }
            if (viewException != null)
            {
                throw viewException;
            }
        }

        public event EventHandler<ViewEventArgs> ViewEventRaise;

        protected void OnViewEventRaise(ViewEventArgs e)
        {
            if (this.ViewEventRaise != null)
            {
                ViewEventRaise(this, e);
            }
        }

        public ICollection<ViewCommander> GetViewCommanders()
        {
            return this._CommandersMap.Values;
        }

        public ViewCommander GetViewCommanderByKey(string commanderKey)
        {
            return this._CommandersMap[commanderKey];
        }

        public void AddViewCommander(ViewCommander viewCommander)
        {
            if (!this._CommandersMap.ContainsKey(viewCommander.CommanderKey))
            {
                this._CommandersMap.Add(viewCommander.CommanderKey, viewCommander);
                this.Commanders.Add(viewCommander);
            }
        }

        public void RemoveViewCommanderByKey(string commanderKey)
        {
            ViewCommander commander = this._CommandersMap[commanderKey];
            if (commander != null)
            {
                this.Commanders.Remove(commander);
                this._CommandersMap.Remove(commanderKey);
            }
        }
        #endregion

        void BaseView_Load(object sender, EventArgs e)
        {
            this.InitializeViewUI();
            ViewEventArgs arg = new ViewEventArgs();
            arg.ViewEventKey = ViewEvents.OnViewLoad;
            this.OnViewEventRaise(arg);
        }
    }
}